package com.tju.resource.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tju.resource.mapper.TbSendMatlMapper;
import com.tju.resource.model.TbSendMatl;
import com.tju.resource.server.TbSendMatlService;
import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.result.ResultEnum;
import com.tju.resource.tool.result.ResultUtil;
import com.tju.resource.tool.tool.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class TbSendMatlServiceImpl extends ServiceImpl<TbSendMatlMapper, TbSendMatl>
        implements TbSendMatlService {
    @Autowired
    private TbSendMatlMapper sendMatlMapper;

    /**
     * 查询发料
     *
     * @param params
     * @return
     */
    @Override
    public Result<Object> selectSendMatl(Map<String, String> params) {
        if (EmptyUtils.isNotEmpty(params.get("page")) && EmptyUtils.isNotEmpty(params.get("limit"))) {
            Page<TbSendMatl> page = new Page<TbSendMatl>(Long.parseLong(params.get("page")), Long.parseLong(params.get("limit")));
            IPage<TbSendMatl> iPage = sendMatlMapper.selectTbSendMatl(page, params);
            List<TbSendMatl> records = iPage.getRecords(); //发料记录
            for (TbSendMatl record : records) {
                if (EmptyUtils.isEmpty(record.getRmName())) { //不为空代表原材料
                    record.setCostOrMatl(true);  //原材料
                }
            }
            return ResultUtil.success(ResultEnum.SUCCESS, records, records.size());
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, "缺少参数!");

    }

    /**
     * 审核发料
     *
     * @param sendMatl
     * @return
     */
    @Override
    public Result<Object> approveSendMatl(TbSendMatl sendMatl) {
        if (EmptyUtils.isNotEmpty(sendMatl.getId())) {
            TbSendMatl tbSendMatl = sendMatlMapper.selectById(sendMatl.getId());
            if (EmptyUtils.isEmpty(tbSendMatl)) return ResultUtil.error(ResultEnum.UNKONW_ERROR, "不存在发料!");
            tbSendMatl.setApproveBy(sendMatl.getApproveBy());//审核人
            tbSendMatl.setApproveStatus(1);
            tbSendMatl.setApproveTime(new Date());
            if (sendMatlMapper.updateById(tbSendMatl) > 0) //修改
                return ResultUtil.success(ResultEnum.SUCCESS, "审核成功!");
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, "审核失败!");
    }

    /**
     * 判断该生产计划下面的发料是否全审核过
     *
     * @param id
     * @return
     */
    @Override
    public Result<Object> approvePlan(Long id) {
        QueryWrapper<TbSendMatl> qw = new QueryWrapper<>();
        qw.eq("pp_id", id);
        List<TbSendMatl> tbSendMatls = sendMatlMapper.selectList(qw);
        for (TbSendMatl tbSendMatl : tbSendMatls)  //循环发料
            if (tbSendMatl.getApproveStatus().equals(0))
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "有没审核的发料!"); //判断有没审核通过就直接返回
        return ResultUtil.success(ResultEnum.SUCCESS, "审核成功!");
    }
}




